 processor 6502
 include "vcs.h"
 include "macro.h"
 include "2600basic.h"
 include "2600basic_variable_redefs.h"
 ifconst bankswitch
  if bankswitch == 8
     ORG $1000
     RORG $D000
  endif
  if bankswitch == 16
     ORG $1000
     RORG $9000
  endif
  if bankswitch == 32
     ORG $1000
     RORG $1000
  endif
 else
   ORG $F000
 endif
; This is a 2-line kernel!
kernel
 sta WSYNC
 lda #255
 sta TIM64T

 lda #1
 sta VDELBL
 sta VDELP0
 ldx ballheight
 inx
 inx
 stx temp4
 lda player1y
 sta temp3

 ifconst shakescreen
   jsr doshakescreen
 else
   ldx missile0height
   inx
 endif

 inx
 stx stack1

 lda bally
 sta stack2

 lda player0y
 ldx #0
 sta WSYNC
 stx GRP0
 stx GRP1
 stx PF1
 stx PF2
 stx CXCLR
 ifconst readpaddle
   stx paddle
 else
   sleep 3
 endif

 sta temp2,x

 ;store these so they can be retrieved later
 ifnconst pfres
   ldx #128-44
 else
   ldx #132-pfres*4
 endif

 inc player1y

 lda missile0y
 sta temp5
 lda missile1y
 sta temp6

 lda playfieldpos
 sta temp1
 
 ifconst pfrowheight
 lda #pfrowheight+2
 else
 ifnconst pfres
   lda #10
 else
   lda #(96/pfres)+2 ; try to come close to the real size
 endif
 endif

 clc
 sbc playfieldpos
 sta playfieldpos
 jmp .startkernel

.skipDrawP0
 lda #0
 tay
 jmp .continueP0

.skipDrawP1
 lda #0
 tay
 jmp .continueP1

.kerloop ; enter at cycle 59??

continuekernel
 sleep 2
continuekernel2
 lda ballheight
 
 ifconst pfres
 ldy playfield+pfres*4-132,x
 sty PF1 ;3
 ldy playfield+pfres*4-131,x
 sty PF2 ;3
 ldy playfield+pfres*4-129,x
 sty PF1 ; 3 too early?
 ldy playfield+pfres*4-130,x
 sty PF2 ;3
 else
 ldy playfield+44-128,x ;4
 sty PF1 ;3
 ldy playfield+45-128,x ;4
 sty PF2 ;3
 ldy playfield+47-128,x ;4
 sty PF1 ; 3 too early?
 ldy playfield+46-128,x;4
 sty PF2 ;3
 endif

 dcp bally
 rol
 rol
; rol
; rol
goback
 sta ENABL 
.startkernel
 lda player1height ;3
 dcp player1y ;5
 bcc .skipDrawP1 ;2
 ldy player1y ;3
 lda (player1pointer),y ;5; player0pointer must be selected carefully by the compiler
			; so it doesn't cross a page boundary!

.continueP1
 sta GRP1 ;3

 ifnconst player1colors
   lda missile1height ;3
   dcp missile1y ;5
   rol;2
   rol;2
   sta ENAM1 ;3
 else
   lda (player1color),y
   sta COLUP1
 ifnconst playercolors
   sleep 7
 else
   lda.w player0colorstore
   sta COLUP0
 endif
 endif

 ifconst pfres
 lda playfield+pfres*4-132,x 
 sta PF1 ;3
 lda playfield+pfres*4-131,x 
 sta PF2 ;3
 lda playfield+pfres*4-129,x 
 sta PF1 ; 3 too early?
 lda playfield+pfres*4-130,x 
 sta PF2 ;3
 else
 lda playfield+44-128,x ;4
 sta PF1 ;3
 lda playfield+45-128,x ;4
 sta PF2 ;3
 lda playfield+47-128,x ;4
 sta PF1 ; 3 too early?
 lda playfield+46-128,x;4
 sta PF2 ;3
 endif 
; sleep 3

 lda player0height
 dcp player0y
 bcc .skipDrawP0
 ldy player0y
 lda (player0pointer),y
.continueP0
 sta GRP0

 ifnconst no_blank_lines
 ifnconst playercolors
   lda missile0height ;3
   dcp missile0y ;5
   sbc stack1
   sta ENAM0 ;3
 else
   lda (player0color),y
   sta player0colorstore
   sleep 6
 endif
   dec temp1
   bne continuekernel
 else
   dec temp1
   beq altkernel2
 ifconst readpaddle
   ldy currentpaddle
   lda INPT0,y
   bpl noreadpaddle
   inc paddle
   jmp continuekernel2
noreadpaddle
   sleep 2
   jmp continuekernel
 else
 ifnconst playercolors 
 ifconst PFcolors
   txa
   tay
   lda (pfcolortable),y
 ifnconst backgroundchange
   sta COLUPF
 else
   sta COLUBK
 endif
   jmp continuekernel
 else
   sleep 12
 endif
 else
   lda (player0color),y
   sta player0colorstore
   sleep 4
 endif
   jmp continuekernel
 endif
altkernel2
   txa
   sbx #252
   bmi lastkernelline
 ifconst pfrowheight
 lda #pfrowheight
 else
 ifnconst pfres
   lda #8
 else
   lda #(96/pfres) ; try to come close to the real size
 endif
 endif
   sta temp1
   jmp continuekernel
 endif

altkernel

 ifconst PFmaskvalue
   lda #PFmaskvalue
 else
   lda #0
 endif
 sta PF1
 sta PF2


 ;sleep 3

 ;28 cycles to fix things
 ;minus 11=17

; lax temp4
; clc
 txa
 sbx #252

 bmi lastkernelline

 ifconst PFcolorandheight
   ldy playfieldcolorandheight-87,x
 ifnconst backgroundchange
   sty COLUPF
 else
   sty COLUBK
 endif
   lda playfieldcolorandheight-88,x
   sta.w temp1
 endif
 ifconst PFheights
   lsr
   lsr
   tay
   lda (pfheighttable),y
   sta.w temp1
 endif
 ifconst PFcolors
   tay
   lda (pfcolortable),y
 ifnconst backgroundchange
   sta COLUPF
 else
   sta COLUBK
 endif
 ifconst pfrowheight
 lda #pfrowheight
 else
 ifnconst pfres
   lda #8
 else
   lda #(96/pfres) ; try to come close to the real size
 endif
 endif
   sta temp1
 endif
 ifnconst PFcolorandheight
 ifnconst PFcolors
 ifnconst PFheights
 ifnconst no_blank_lines
 ; read paddle 0
 ; lo-res paddle read
  ; bit INPT0
  ; bmi paddleskipread
  ; inc paddle0
;donepaddleskip
   sleep 10
 ifconst pfrowheight
   lda #pfrowheight
 else
 ifnconst pfres
   lda #8
 else
   lda #(96/pfres) ; try to come close to the real size
 endif
 endif
   sta temp1
 endif
 endif
 endif
 endif
 

 lda ballheight
 dcp bally
 sbc temp4


 jmp goback


 ifnconst no_blank_lines
lastkernelline
 ifnconst PFcolors
   sleep 10
 else
   ldy #124
   lda (pfcolortable),y
   sta COLUPF
 endif

 ifconst PFheights
 ldx #1
 sleep 4
 else
 ldx playfieldpos
 sleep 3
 endif

 jmp enterlastkernel

 else
lastkernelline
 
 ifconst PFheights
 ldx #1
 sleep 5
 else
   ldx playfieldpos
 sleep 4
 endif

   cpx #1
   bne .enterfromNBL
   jmp no_blank_lines_bailout
 endif

 if ((<*)>$d5)
 align 256
 endif
 ; this is a kludge to prevent page wrapping - fix!!!

.skipDrawlastP1
 sleep 2
 lda #0
 jmp .continuelastP1

.endkerloop ; enter at cycle 59??
 
 nop

.enterfromNBL
 ifconst pfres
 ldy.w playfield+pfres*4-4
 sty PF1 ;3
 ldy.w playfield+pfres*4-3
 sty PF2 ;3
 ldy.w playfield+pfres*4-1
 sty PF1 ; possibly too early?
 ldy.w playfield+pfres*4-2
 sty PF2 ;3
 else
 ldy.w playfield+44
 sty PF1 ;3
 ldy.w playfield+45
 sty PF2 ;3
 ldy.w playfield+47
 sty PF1 ; possibly too early?
 ldy.w playfield+46
 sty PF2 ;3
 endif

enterlastkernel
 lda ballheight

; tya
 dcp bally
; sleep 4

; sbc stack3
 rol
 rol
 sta ENABL 

 lda player1height ;3
 dcp player1y ;5
 bcc .skipDrawlastP1
 ldy player1y ;3
 lda (player1pointer),y ;5; player0pointer must be selected carefully by the compiler
			; so it doesn't cross a page boundary!

.continuelastP1
 sta GRP1 ;3

 ifnconst player1colors
   lda missile1height ;3
   dcp missile1y ;5
 else
   lda (player1color),y
   sta COLUP1
 endif

 dex
 ;dec temp4 ; might try putting this above PF writes
 beq endkernel


 ifconst pfres
 ldy.w playfield+pfres*4-4
 sty PF1 ;3
 ldy.w playfield+pfres*4-3
 sty PF2 ;3
 ldy.w playfield+pfres*4-1
 sty PF1 ; possibly too early?
 ldy.w playfield+pfres*4-2
 sty PF2 ;3
 else
 ldy.w playfield+44
 sty PF1 ;3
 ldy.w playfield+45
 sty PF2 ;3
 ldy.w playfield+47
 sty PF1 ; possibly too early?
 ldy.w playfield+46
 sty PF2 ;3
 endif

 ifnconst player1colors
   rol;2
   rol;2
   sta ENAM1 ;3
 else
 ifnconst playercolors
   sleep 7
 else
   lda.w player0colorstore
   sta COLUP0
 endif
 endif
 
 lda.w player0height
 dcp player0y
 bcc .skipDrawlastP0
 ldy player0y
 lda (player0pointer),y
.continuelastP0
 sta GRP0



 ifnconst no_blank_lines
   lda missile0height ;3
   dcp missile0y ;5
   sbc stack1
   sta ENAM0 ;3
   jmp .endkerloop
 else
 ifconst readpaddle
   ldy currentpaddle
   lda INPT0,y
   bpl noreadpaddle2
   inc paddle
   jmp .endkerloop
noreadpaddle2
   sleep 4
   jmp .endkerloop
 else ; no_blank_lines and no paddle reading
 sleep 14
 jmp .endkerloop
 endif
 endif


;  ifconst donepaddleskip
;paddleskipread
 ; this is kind of lame, since it requires 4 cycles from a page boundary crossing
 ; plus we get a lo-res paddle read
; bmi donepaddleskip
;  endif

.skipDrawlastP0
 sleep 2
 lda #0
 jmp .continuelastP0

 ifconst no_blank_lines
no_blank_lines_bailout
 ldx #0
 endif

endkernel
 ; 6 digit score routine
 stx PF1
 stx PF2
 stx PF0
 clc

 ifconst pfrowheight
 lda #pfrowheight+2
 else
 ifnconst pfres
   lda #10
 else
   lda #(96/pfres)+2 ; try to come close to the real size
 endif
 endif

 sbc playfieldpos
 sta playfieldpos
 txa

 ifconst shakescreen
   bit shakescreen
   bmi noshakescreen2
   ldx #$3D
noshakescreen2
 endif

   sta WSYNC,x

;                STA WSYNC ;first one, need one more
 sta REFP0
 sta REFP1
                STA GRP0
                STA GRP1
 ;               STA PF1
   ;             STA PF2
 sta HMCLR
 sta ENAM0
 sta ENAM1
 sta ENABL

 lda temp2 ;restore variables that were obliterated by kernel
 sta player0y
 lda temp3
 sta player1y
 ifnconst player1colors
   lda temp6
   sta missile1y
 endif
 ifnconst playercolors
 ifnconst readpaddle
   lda temp5
   sta missile0y
 endif
 endif
 lda stack2
 sta bally

 ifconst no_blank_lines
 sta WSYNC
 endif

 lda INTIM
 clc
 ifnconst vblank_time
 adc #43+12+87
 else
 adc #vblank_time+12+87
 endif
; sta WSYNC
 sta TIM64T

 ifconst minikernel
 jsr minikernel
 endif

 ; now reassign temp vars for score pointers

; score pointers contain:
; score1-5: lo1,lo2,lo3,lo4,lo5,lo6
; swap lo2->temp1
; swap lo4->temp3
; swap lo6->temp5
 ifnconst noscore
 lda scorepointers+1
; ldy temp1
 sta temp1
; sty scorepointers+1

 lda scorepointers+3
; ldy temp3
 sta temp3
; sty scorepointers+3


 sta HMCLR
 tsx
 stx stack1 
 ldx #$10
 stx HMP0

 sta WSYNC
 ldx #0
                STx GRP0
                STx GRP1 ; seems to be needed because of vdel

 lda scorepointers+5
; ldy temp5
 sta temp5,x
; sty scorepointers+5
 lda #>scoretable
 sta scorepointers+1
 sta scorepointers+3
 sta scorepointers+5,x
 sta temp2,x
 sta temp4,x
 sta temp6,x
                LDY #7
                STA RESP0
                STA RESP1


        LDA #$03
        STA NUSIZ0
        STA NUSIZ1,x
        STA VDELP0
        STA VDELP1
        LDA #$20
        STA HMP1
               LDA scorecolor 
;               STA HMCLR
;               STA WSYNC; second one
                STA HMOVE ; cycle 73 ?

                STA COLUP0
                STA COLUP1
 lda  (scorepointers),y
 sta  GRP0
 ifconst pfscore
 lda pfscorecolor
 sta COLUPF
 endif
 lda  (scorepointers+8),y
 sta WSYNC
 sleep 2
 jmp beginscore

 if ((<*)>$d4)
 align 256 ; kludge that potentially wastes space!  should be fixed!
 endif

loop2
 lda  (scorepointers),y     ;+5  68  204
 sta  GRP0            ;+3  71  213      D1     --      --     --
 ifconst pfscore
 lda.w pfscore1
 sta PF1
 else
 sleep 7
 endif
 ; cycle 0
 lda  (scorepointers+$8),y  ;+5   5   15
beginscore
 sta  GRP1            ;+3   8   24      D1     D1      D2     --
 lda  (scorepointers+$6),y  ;+5  13   39
 sta  GRP0            ;+3  16   48      D3     D1      D2     D2
 lax  (scorepointers+$2),y  ;+5  29   87
 txs
 lax  (scorepointers+$4),y  ;+5  36  108
 sleep 3

 ifconst pfscore
 lda pfscore2
 sta PF1
 else
 sleep 6
 endif

 lda  (scorepointers+$A),y  ;+5  21   63
 stx  GRP1            ;+3  44  132      D3     D3      D4     D2!
 tsx
 stx  GRP0            ;+3  47  141      D5     D3!     D4     D4
 sta  GRP1            ;+3  50  150      D5     D5      D6     D4!
 sty  GRP0            ;+3  53  159      D4*    D5!     D6     D6
 dey
 bpl  loop2           ;+2  60  180

 ldx stack1 
 txs
; lda scorepointers+1
 ldy temp1
; sta temp1
 sty scorepointers+1

                LDA #0   
 sta PF1
               STA GRP0
                STA GRP1
        STA VDELP0
        STA VDELP1;do we need these
        STA NUSIZ0
        STA NUSIZ1

; lda scorepointers+3
 ldy temp3
; sta temp3
 sty scorepointers+3

; lda scorepointers+5
 ldy temp5
; sta temp5
 sty scorepointers+5
 endif ;noscore
 LDA #%11000010
 sta WSYNC
 STA VBLANK
 RETURN

 ifconst shakescreen
doshakescreen
   bit shakescreen
   bmi noshakescreen
   sta WSYNC
noshakescreen
   ldx missile0height
   inx
   rts
 endif

start
 sei
 cld
 ldy #0
 lda $D0
 cmp #$2C               ;check RAM location #1
 bne MachineIs2600
 lda $D1
 cmp #$A9               ;check RAM location #2
 bne MachineIs2600
 dey
MachineIs2600
 ldx #0
 txa
clearmem
 inx
 txs
 pha
 bne clearmem
 sty temp1
 ifconst pfrowheight
 lda pfrowheight
 else
 ifconst pfres
 lda #(96/pfres)
 else
 lda #8
 endif
 endif
 sta playfieldpos
 ldx #5
initscore
 lda #<scoretable
 sta scorepointers,x 
 dex
 bpl initscore
 lda #1
 sta CTRLPF
 ora INTIM
 sta rand

 ifconst multisprite
   jsr multisprite_setup
 endif

 ifnconst bankswitch
   jmp game
 else
   lda #>(game-1)
   pha
   lda #<(game-1)
   pha
   pha
   pha
   ldx #1
   jmp BS_jsr
 endif
; playfield drawing routines
; you get a 32x12 bitmapped display in a single color :)
; 0-31 and 0-11

pfclear ; clears playfield - or fill with pattern
 ifconst pfres
 ldx #pfres*4-1
 else
 ldx #47
 endif
pfclear_loop
 ifnconst superchip
 sta playfield,x
 else
 sta playfield-128,x
 endif
 dex
 bpl pfclear_loop
 RETURN
 
setuppointers
 stx temp2 ; store on.off.flip value
 tax ; put x-value in x 
 lsr
 lsr
 lsr ; divide x pos by 8 
 sta temp1
 tya
 asl
 asl ; multiply y pos by 4
 clc
 adc temp1 ; add them together to get actual memory location offset
 tay ; put the value in y
 lda temp2 ; restore on.off.flip value
 rts

pfread
;x=xvalue, y=yvalue
 jsr setuppointers
 lda setbyte,x
 and playfield,y
 eor setbyte,x
; beq readzero
; lda #1
; readzero
 RETURN

pfpixel
;x=xvalue, y=yvalue, a=0,1,2
 jsr setuppointers

 ifconst bankswitch
 lda temp2 ; load on.off.flip value (0,1, or 2)
 beq pixelon_r  ; if "on" go to on
 lsr
 bcs pixeloff_r ; value is 1 if true
 lda playfield,y ; if here, it's "flip"
 eor setbyte,x
 ifconst superchip
 sta playfield-128,y
 else
 sta playfield,y
 endif
 RETURN
pixelon_r
 lda playfield,y
 ora setbyte,x
 ifconst superchip
 sta playfield-128,y
 else
 sta playfield,y
 endif
 RETURN
pixeloff_r
 lda setbyte,x
 eor #$ff
 and playfield,y
 ifconst superchip
 sta playfield-128,y
 else
 sta playfield,y
 endif
 RETURN

 else
 jmp plotpoint
 endif

pfhline
;x=xvalue, y=yvalue, a=0,1,2, temp3=endx
 jsr setuppointers
 jmp noinc
keepgoing
 inx
 txa
 and #7
 bne noinc
 iny
noinc
 jsr plotpoint
 cpx temp3
 bmi keepgoing
 RETURN

pfvline
;x=xvalue, y=yvalue, a=0,1,2, temp3=endx
 jsr setuppointers
 sty temp1 ; store memory location offset
 inc temp3 ; increase final x by 1 
 lda temp3
 asl
 asl ; multiply by 4
 sta temp3 ; store it
 ; Thanks to Michael Rideout for fixing a bug in this code
 ; right now, temp1=y=starting memory location, temp3=final
 ; x should equal original x value
keepgoingy
 jsr plotpoint
 iny
 iny
 iny
 iny
 cpy temp3
 bmi keepgoingy
 RETURN

plotpoint
 lda temp2 ; load on.off.flip value (0,1, or 2)
 beq pixelon  ; if "on" go to on
 lsr
 bcs pixeloff ; value is 1 if true
 lda playfield,y ; if here, it's "flip"
 eor setbyte,x
  ifconst superchip
 sta playfield-128,y
 else
 sta playfield,y
 endif
 rts
pixelon
 lda playfield,y
 ora setbyte,x
 ifconst superchip
 sta playfield-128,y
 else
 sta playfield,y
 endif
 rts
pixeloff
 lda setbyte,x
 eor #$ff
 and playfield,y
 ifconst superchip
 sta playfield-128,y
 else
 sta playfield,y
 endif
 rts

setbyte
 .byte $80
 .byte $40
 .byte $20
 .byte $10
 .byte $08
 .byte $04
 .byte $02
 .byte $01
 .byte $01
 .byte $02
 .byte $04
 .byte $08
 .byte $10
 .byte $20
 .byte $40
 .byte $80
 .byte $80
 .byte $40
 .byte $20
 .byte $10
 .byte $08
 .byte $04
 .byte $02
 .byte $01
 .byte $01
 .byte $02
 .byte $04
 .byte $08
 .byte $10
 .byte $20
 .byte $40
 .byte $80
pfscroll ;(a=0 left, 1 right, 2 up, 4 down, 6=upup, 12=downdown)
 bne notleft
;left
 ifconst pfres
 ldx #pfres*4
 else
 ldx #48
 endif
leftloop
 lda playfield-1,x
 lsr

 ifconst superchip
 lda playfield-2,x
 rol
 sta playfield-130,x
 lda playfield-3,x
 ror
 sta playfield-131,x
 lda playfield-4,x
 rol
 sta playfield-132,x
 lda playfield-1,x
 ror
 sta playfield-129,x
 else
 rol playfield-2,x
 ror playfield-3,x
 rol playfield-4,x
 ror playfield-1,x
 endif

 txa
 sbx #4
 bne leftloop
 RETURN

notleft
 lsr
 bcc notright
;right

 ifconst pfres
 ldx #pfres*4
 else
 ldx #48
 endif
rightloop
 lda playfield-4,x
 lsr
 ifconst superchip
 lda playfield-3,x
 rol
 sta playfield-131,x
 lda playfield-2,x
 ror
 sta playfield-130,x
 lda playfield-1,x
 rol
 sta playfield-129,x
 lda playfield-4,x
 ror
 sta playfield-132,x
 else
 rol playfield-3,x
 ror playfield-2,x
 rol playfield-1,x
 ror playfield-4,x
 endif
 txa
 sbx #4
 bne rightloop
  RETURN

notright
 lsr
 bcc notup
;up
 lsr
 bcc onedecup
 dec playfieldpos
onedecup
 dec playfieldpos
 beq shiftdown 
 bpl noshiftdown2 
shiftdown
  ifconst pfrowheight
 lda #pfrowheight
 else
 ifnconst pfres
   lda #8
 else
   lda #(96/pfres) ; try to come close to the real size
 endif
 endif

 sta playfieldpos
 lda playfield+3
 sta temp4
 lda playfield+2
 sta temp3
 lda playfield+1
 sta temp2
 lda playfield
 sta temp1
 ldx #0
up2
 lda playfield+4,x
 ifconst superchip
 sta playfield-128,x
 lda playfield+5,x
 sta playfield-127,x
 lda playfield+6,x
 sta playfield-126,x
 lda playfield+7,x
 sta playfield-125,x
 else
 sta playfield,x
 lda playfield+5,x
 sta playfield+1,x
 lda playfield+6,x
 sta playfield+2,x
 lda playfield+7,x
 sta playfield+3,x
 endif
 txa
 sbx #252
 ifconst pfres
 cpx #(pfres-1)*4
 else
 cpx #44
 endif
 bne up2

 lda temp4
 
 ifconst superchip
 ifconst pfres
 sta playfield+pfres*4-129
 lda temp3
 sta playfield+pfres*4-130
 lda temp2
 sta playfield+pfres*4-131
 lda temp1
 sta playfield+pfres*4-132
 else
 sta playfield+47-128
 lda temp3
 sta playfield+46-128
 lda temp2
 sta playfield+45-128
 lda temp1
 sta playfield+44-128
 endif
 else
 ifconst pfres
 sta playfield+pfres*4-1
 lda temp3
 sta playfield+pfres*4-2
 lda temp2
 sta playfield+pfres*4-3
 lda temp1
 sta playfield+pfres*4-4
 else
 sta playfield+47
 lda temp3
 sta playfield+46
 lda temp2
 sta playfield+45
 lda temp1
 sta playfield+44
 endif
 endif
noshiftdown2
 RETURN


notup
;down
 lsr
 bcs oneincup
 inc playfieldpos
oneincup
 inc playfieldpos
 lda playfieldpos

  ifconst pfrowheight
 cmp #pfrowheight+1
 else
 ifnconst pfres
   cmp #9
 else
   cmp #(96/pfres)+1 ; try to come close to the real size
 endif
 endif

 bcc noshiftdown 
 lda #1
 sta playfieldpos

 ifconst pfres
 lda playfield+pfres*4-1
 sta temp4
 lda playfield+pfres*4-2
 sta temp3
 lda playfield+pfres*4-3
 sta temp2
 lda playfield+pfres*4-4
 else
 lda playfield+47
 sta temp4
 lda playfield+46
 sta temp3
 lda playfield+45
 sta temp2
 lda playfield+44
 endif

 sta temp1

 ifconst pfres
 ldx #(pfres-1)*4
 else
 ldx #44
 endif
down2
 lda playfield-1,x
 ifconst superchip
 sta playfield-125,x
 lda playfield-2,x
 sta playfield-126,x
 lda playfield-3,x
 sta playfield-127,x
 lda playfield-4,x
 sta playfield-128,x
 else
 sta playfield+3,x
 lda playfield-2,x
 sta playfield+2,x
 lda playfield-3,x
 sta playfield+1,x
 lda playfield-4,x
 sta playfield,x
 endif
 txa
 sbx #4
 bne down2

 lda temp4
 ifconst superchip
 sta playfield-125
 lda temp3
 sta playfield-126
 lda temp2
 sta playfield-127
 lda temp1
 sta playfield-128
 else
 sta playfield+3
 lda temp3
 sta playfield+2
 lda temp2
 sta playfield+1
 lda temp1
 sta playfield
 endif
noshiftdown
 RETURN
;standard routines needed for pretty much all games
; just the random number generator is left - maybe we should remove this asm file altogether?
; repositioning code and score pointer setup moved to overscan
; read switches, joysticks now compiler generated (more efficient)

randomize
	lda rand
	lsr
 ifconst rand16
	rol rand16
 endif
	bcc noeor
	eor #$B4
noeor
	sta rand
 ifconst rand16
	eor rand16
 endif
	RETURN
drawscreen
 ifconst debugscore
   ldx #14
   lda INTIM ; display # cycles left in the score

 ifconst mincycles
 lda mincycles 
 cmp INTIM
 lda mincycles
 bcc nochange
 lda INTIM
 sta mincycles
nochange
 endif

;   cmp #$2B
;   bcs no_cycles_left
   bmi cycles_left
   ldx #64
   eor #$ff ;make negative
cycles_left
   stx scorecolor
   and #$7f ; clear sign bit
   tax
   lda scorebcd,x
   sta score+2
   lda scorebcd1,x
   sta score+1
   jmp done_debugscore   
scorebcd
 .byte $00, $64, $28, $92, $56, $20, $84, $48, $12, $76, $40
 .byte $04, $68, $32, $96, $60, $24, $88, $52, $16, $80, $44
 .byte $08, $72, $36, $00, $64, $28, $92, $56, $20, $84, $48
 .byte $12, $76, $40, $04, $68, $32, $96, $60, $24, $88
scorebcd1
 .byte 0, 0, 1, 1, 2, 3, 3, 4, 5, 5, 6
 .byte 7, 7, 8, 8, 9, $10, $10, $11, $12, $12, $13
 .byte $14, $14, $15, $16, $16, $17, $17, $18, $19, $19, $20
 .byte $21, $21, $22, $23, $23, $24, $24, $25, $26, $26
done_debugscore
 endif

 ifconst debugcycles
   lda INTIM ; if we go over, it mucks up the background color
;   cmp #$2B
;   BCC overscan
   bmi overscan
   sta COLUBK
   bcs doneoverscan
 endif

 
overscan
 lda INTIM ;wait for sync
 bmi overscan
doneoverscan
;do VSYNC
 lda #2
 sta WSYNC
 sta VSYNC
 STA WSYNC
 STA WSYNC
 LDA #0
 STA WSYNC
 STA VSYNC
 sta VBLANK
 ifnconst overscan_time
 lda #37+128
 else
 lda #overscan_time+128
 endif
 sta TIM64T

 ifconst legacy
 if legacy < 100
 ldx #4
adjustloop
 lda player0x,x
 sec
 sbc #14 ;?
 sta player0x,x
 dex
 bpl adjustloop
 endif
 endif
 if (<*)>$F0
 align 256, $EA
 endif
  sta WSYNC
  ldx #4
  SLEEP 3
HorPosLoop       ;     5
  lda player0x,X  ;+4   9
  sec           ;+2  11
DivideLoop
  sbc #15
  bcs DivideLoop;+4  15
  sta temp1,X    ;+4  19
  sta RESP0,X   ;+4  23
  sta WSYNC
  dex
  bpl HorPosLoop;+5   5
                ;     4

  ldx #4
  ldy temp1,X
  lda repostable-256,Y
  sta HMP0,X    ;+14 18

  dex
  ldy temp1,X
  lda repostable-256,Y
  sta HMP0,X    ;+14 32

  dex
  ldy temp1,X
  lda repostable-256,Y
  sta HMP0,X    ;+14 46

  dex
  ldy temp1,X
  lda repostable-256,Y
  sta HMP0,X    ;+14 60

  dex
  ldy temp1,X
  lda repostable-256,Y
  sta HMP0,X    ;+14 74

  sta WSYNC
 
  sta HMOVE     ;+3   3


 ifconst legacy
 if legacy < 100
 ldx #4
adjustloop2
 lda player0x,x
 clc
 adc #14 ;?
 sta player0x,x
 dex
 bpl adjustloop2
 endif
 endif




;set score pointers
 lax score+2
 jsr scorepointerset
 sty scorepointers+5
 stx scorepointers+2
 lax score+1
 jsr scorepointerset
 sty scorepointers+4
 stx scorepointers+1
 lax score
 jsr scorepointerset
 sty scorepointers+3
 stx scorepointers

vblk
; run possible vblank bB code
 ifconst vblank_bB_code
   jsr vblank_bB_code
 endif
vblk2
 LDA INTIM
 bmi vblk2
 jmp kernel
 

    .byte $80,$70,$60,$50,$40,$30,$20,$10,$00
    .byte $F0,$E0,$D0,$C0,$B0,$A0,$90
repostable

scorepointerset
 and #$0F
 asl
 asl
 asl
 adc #<scoretable
 tay 
 txa
; and #$F0
; lsr
 asr #$F0
 adc #<scoretable
 tax
 rts
; Fixed point math routines - created by AtariAge member djmips
; some changes by Fred Quimby

;assignment from 8.8 to 4.4

Assign88to44:

      ; A(4.4) = A,X(8.8)

       stx temp1
       rol temp1
       asl
       rol temp1
       asl
       rol temp1
       asl
       rol temp1
       asl
       rts

;assignment from 4.4 to 8.8
;

Assign44to88:

      ; A,X(8.8) = A(4.4)

       sta temp1
       lda #0
       asl temp1
       sbc #0   ;
       eor #$ff ; do sign extend
       rol
       asl temp1
       rol
       asl temp1
       rol
       asl temp1
       rol
       ldx temp1
       rts

 ifconst bankswitch
Assign88to44bs:

      ; A(4.4) = A,X(8.8)

       stx temp1
       rol temp1
       asl
       rol temp1
       asl
       rol temp1
       asl
       rol temp1
       asl
       RETURN

;assignment from 4.4 to 8.8
;

Assign44to88bs:

      ; A,X(8.8) = A(4.4)

       sta temp1
       lda #0
       asl temp1
       sbc #0   ;
       eor #$ff ; do sign extend
       rol
       asl temp1
       rol
       asl temp1
       rol
       asl temp1
       rol
       ldx temp1
       RETURN
 endif

;
;Addition/subtraction asm procedures:

;add/sub 8.8 to/from 4.4

Add88to44:

      ; A(4.4) = A,X(8.8) + Y(4.4)

       jsr Assign88to44
       sty temp1
       clc
       adc temp1
       rts

Sub88from44:

      ; A(4.4) = A,X(8.8) - Y(4.4)

       jsr Assign88to44
       sty temp1
       sec
       sbc temp1
       rts


Add44to88:

      ; A,X(8.8) = A,X(8.8) + Y(4.4)

       sta temp2
       stx temp3
       tya
       jsr Assign44to88
       clc
       sta temp1
       txa
       adc temp3
       tax
       lda temp1
       adc temp2
       rts


Sub44from88:

      ; A,X(8.8) = A,X(8.8) - Y(4.4)

       sta temp2
       stx temp3
       tya
       jsr Assign44to88
       sec
       sta temp1
       lda temp3
       stx temp3
       sbc temp3
       tax
       lda temp2
       sbc temp1
       rts

game
.L00 ;  include fixed_point_math.asm

.L01 ;  rem pikachu Chase

.L02 ;  rem A fun game that may help you learn batari Basic!

.L03 ;  rem

.
 ; 

.L04 ;  rem timed game, 16 levels

.L05 ;  rem Each level lasts about one minute

.L06 ;  rem you must score 1000 points to move on

.L07 ;  rem COLOR/BW switch selects joystick or DC

.L08 ;  rem left difficulty A=stop on collision; B=slow down on collision

.L09 ;  rem right difficulty A=L/R border; B=no border

.
 ; 

.L010 ;  set kernel_options no_blank_lines player1colors

.L011 ;  playfieldpos = 4

	LDA #4
	STA playfieldpos
.L012 ;  set smartbranching on

.
 ; 

.L013 ;  dim carpos = a

.L014 ;  dim turndelay = b

.L015 ;  dim collcount = b

.L016 ;  dim carframe = c

.L017 ;  dim gamebits = e

.L018 ;  dim velocity = f.f

.L019 ;  dim xvelocity = g.g

.L020 ;  dim yvelocity = h.h

.L021 ;  dim tempvel = i.i

.L022 ;  dim finalxvelocity = l.l

.L023 ;  dim finalyvelocity = m.m

.
 ; 

.L024 ;  dim p0x = player0x.j

.L025 ;  dim p0y = player0y.k

.L026 ;  dim last = n

.
 ; 

.L027 ;  dim scadd = o

.L028 ;  dim timer1 = p

.L029 ;  dim timer2 = q

.L030 ;  dim level = q

.L031 ;  rem level bits

.L032 ;  rem bit 0: pikachu speed (slow/fast)

.L033 ;  rem bit 1: pikachu movement (random/run away)

.L034 ;  rem bit 2: car speed (slow/fast)

.L035 ;  rem bit 3: road surface (pavement/ice)

.
 ; 

.L036 ;  dim tempvel8 = temp1.temp2

.
 ; 

.L037 ;  dim pikachuvel = temp5.temp6

.L038 ;  dim pikachuxvel = r

.L039 ;  dim pikachuyvel = s

.L040 ;  dim pikachufinalxvel = t

.L041 ;  dim pikachufinalyvel = u

.L042 ;  dim pikachuxpos = player1x.v

.L043 ;  dim pikachuypos = player1y.w

.
 ; 

.L044 ;  dim sc1 = score

.L045 ;  dim sc2 = score + 1

.
 ; 

.L046 ;  rem 

.L047 ;  rem velocity doesn't change when direction changes

.L048 ;  rem xvelocity and yvelocity change

.L049 ;  rem they change instantly when velocity <= 0.5 max

.L050 ;  rem they change gradually when velocity > 0.5 max

.
 ; 

.L051 ;  carpos  =  4

	LDA #4
	STA carpos
.L052 ;  rem turndelay = 0

.L053 ;  player0x  =  40  :  player0y  =  40

	LDA #40
	STA player0x
	STA player0y
.startLoop
 ; startLoop

.L054 ;  if turndelay{1} then player1: 

	LDA turndelay
	AND #2
	BEQ .skipL054
.condpart0
	LDA #<player0then_1

	STA player1pointerlo
	LDA #>player0then_1

	STA player1pointerhi
	LDA #8
	STA player1height
.skipL054
.L055 ;  if !turndelay{1} then player1:

	LDA turndelay
	AND #2
	BNE .skipL055
.condpart1
	LDA #<player1then_1

	STA player1pointerlo
	LDA #>player1then_1

	STA player1pointerhi
	LDA #8
	STA player1height
.skipL055
.
 ; 

.L056 ;  player1color:

	LDA #<playercolorL056_1

	STA player1color
	LDA #>playercolorL056_1

	STA player1color+1
.L057 ;  scorecolor = 30

	LDA #30
	STA scorecolor
.L058 ;  if switchreset then reboot

 lda #1
 bit SWCHB
	BNE .skipL058
.condpart2
	JMP ($FFFC)
.skipL058
.L059 ;  if switchrightb then PF0 = 0 else PF0 = 63

 lda #$80
 bit SWCHB
	BNE .skipL059
.condpart3
	LDA #0
	STA PF0
 jmp .skipelse0
.skipL059
	LDA #63
	STA PF0
.skipelse0
.L060 ;  COLUPF =  ( level  *  16 )  ^ 244

; complex statement detected
	LDA level
	asl
	asl
	asl
	asl
	EOR #244
	STA COLUPF
.L061 ;  if gamebits{7} then gamerunning

	BIT gamebits
 if ((* - .gamerunning) < 127) && ((* - .gamerunning) > -128)
	bmi .gamerunning
 else
	bpl .0skipgamerunning
	jmp .gamerunning
.0skipgamerunning
 endif
.L062 ;  if !joy0fire then timer1 = timer1 + 1

 lda #$80
 bit INPT4
	BEQ .skipL062
.condpart4
	INC timer1
.skipL062
.L063 ;  if timer1 = 0 then nostartgame

	LDA timer1
	CMP #0
 if ((* - .nostartgame) < 127) && ((* - .nostartgame) > -128)
	BEQ .nostartgame
 else
	bne .1skipnostartgame
	jmp .nostartgame
.1skipnostartgame
 endif
.L064 ;  if joy0fire then score = 0 : timer1 = 0 : timer2 = 0 : gamebits{7} = 1 : pfclear

 lda #$80
 bit INPT4
	BNE .skipL064
.condpart5
	LDA #$00
	STA score+2
	LDA #$00
	STA score+1
	LDA #$00
	STA score
	LDA #0
	STA timer1
	STA timer2
	LDA gamebits
	ORA #128
	STA gamebits
	LDA #0
 jsr pfclear
.skipL064
.nostartgame
 ; nostartgame

.L065 ;  AUDV0 = 0 : AUDV1 = 0 : goto hitwall

	LDA #0
	STA AUDV0
	STA AUDV1
 jmp .hitwall

.gamerunning
 ; gamerunning

.L066 ;  timer1 = timer1 + 1 : if timer1 = 0 then timer2 = timer2 + $10

	INC timer1
	LDA timer1
	CMP #0
     BNE .skipL066
.condpart6
	LDA timer2
	CLC
	ADC #$10
	STA timer2
.skipL066
.L067 ;  if timer2 < $C0 then notendlevel

	LDA timer2
	CMP #$C0
 if ((* - .notendlevel) < 127) && ((* - .notendlevel) > -128)
	bcc .notendlevel
 else
	bcs .2skipnotendlevel
	jmp .notendlevel
.2skipnotendlevel
 endif
.L068 ;  temp1 = level  &  $0F

	LDA level
	AND #$0F
	STA temp1
.L069 ;  temp2 = sc1 * 16 + sc2 / 16

; complex statement detected
	LDA sc1
	asl
	asl
	asl
	asl
	PHA
	LDA sc2
	lsr
	lsr
	lsr
	lsr
	TSX
	INX
	TXS
	CLC
	ADC $100,x
	STA temp2
.L070 ;  temp1 = level  &  $0F

	LDA level
	AND #$0F
	STA temp1
.L071 ;  if temp2 < gonextlevel[temp1]  &&  timer1{5} then scorecolor = 64

	LDA temp2
	LDX temp1
	CMP gonextlevel,x
     BCS .skipL071
.condpart7
	LDA timer1
	AND #32
	BEQ .skip7then
.condpart8
	LDA #64
	STA scorecolor
.skip7then
.skipL071
.L072 ;  if timer2 < $F0 then notendlevel

	LDA timer2
	CMP #$F0
 if ((* - .notendlevel) < 127) && ((* - .notendlevel) > -128)
	bcc .notendlevel
 else
	bcs .3skipnotendlevel
	jmp .notendlevel
.3skipnotendlevel
 endif
.L073 ;  if temp2 >= gonextlevel[temp1] then level = level + $11 : pfclear else gamebits{7} = 0

	LDA temp2
	LDX temp1
	CMP gonextlevel,x
     BCC .skipL073
.condpart9
	LDA level
	CLC
	ADC #$11
	STA level
	LDA #0
 jsr pfclear
 jmp .skipelse1
.skipL073
	LDA gamebits
	AND #127
	STA gamebits
.skipelse1
.notendlevel
 ; notendlevel

.L074 ;  gosub movepikachu

 jsr .movepikachu

.L075 ;  if collcount < 16 then skipcrashsound

	LDA collcount
	CMP #16
 if ((* - .skipcrashsound) < 127) && ((* - .skipcrashsound) > -128)
	bcc .skipcrashsound
 else
	bcs .4skipskipcrashsound
	jmp .skipcrashsound
.4skipskipcrashsound
 endif
.L076 ;  collcount = collcount - 16

	LDA collcount
	SEC
	SBC #16
	STA collcount
.L077 ;  AUDV0 = collcount / 16

	LDA collcount
	lsr
	lsr
	lsr
	lsr
	STA AUDV0
.L078 ;  AUDC0 = 8

	LDA #8
	STA AUDC0
.L079 ;  if collcount{3} then AUDF0 =  ( collcount & rand )  / 8 else AUDF0 = 17

	LDA collcount
	AND #8
	BEQ .skipL079
.condpart10
; complex statement detected
	LDA collcount
	AND rand
	lsr
	lsr
	lsr
	STA AUDF0
 jmp .skipelse2
.skipL079
	LDA #17
	STA AUDF0
.skipelse2
.L080 ;  goto skipenginesound

 jmp .skipenginesound

.skipcrashsound
 ; skipcrashsound

.L081 ;  collcount{3} = 0

	LDA collcount
	AND #247
	STA collcount
.L082 ;  AUDV0 = 10 : AUDC0 = 2

	LDA #10
	STA AUDV0
	LDA #2
	STA AUDC0
.L083 ;  AUDF0 = 18 - f / 4 : if f > 67 then AUDF0 = 1

; complex statement detected
	LDA #18
	PHA
	LDA f
	lsr
	lsr
	TAY
	PLA
	TSX
	STY $00,x
	SEC
	SBC $100,x
	STA AUDF0
	LDA #67
	CMP f
     BCS .skipL083
.condpart11
	LDA #1
	STA AUDF0
.skipL083
.skipenginesound
 ; skipenginesound

.
 ; 

.L084 ;  if joy0fire then velocity = velocity + 0.0625 : goto nomove1

 lda #$80
 bit INPT4
	BNE .skipL084
.condpart12
	CLC
	LDA velocity
	ADC #1
	STA velocity
 jmp .nomove1

.skipL084
.L085 ;  gamebits = gamebits ^ %00000100

	LDA gamebits
	EOR #%00000100
	STA gamebits
.L086 ;  if gamebits{2} then nomove1

	LDA gamebits
	AND #4
 if ((* - .nomove1) < 127) && ((* - .nomove1) > -128)
	BNE .nomove1
 else
	beq .5skipnomove1
	jmp .nomove1
.5skipnomove1
 endif
.L087 ;  velocity = velocity - 0.0625

	SEC
	LDA velocity
	SBC #1
	STA velocity
.nomove1
 ; nomove1

.L088 ;  if level{2} then temp1 = 96 else temp1 = 64

	LDA level
	AND #4
	BEQ .skipL088
.condpart13
	LDA #96
	STA temp1
 jmp .skipelse3
.skipL088
	LDA #64
	STA temp1
.skipelse3
.L089 ;  if gamebits{4} then temp1 = 80

	LDA gamebits
	AND #16
	BEQ .skipL089
.condpart14
	LDA #80
	STA temp1
.skipL089
.L090 ;  if velocity  >  temp1  &&  velocity  <  192 then velocity = velocity - 0.0625

	LDA temp1
	CMP velocity
     BCS .skipL090
.condpart15
	LDA velocity
	CMP #192
     BCS .skip15then
.condpart16
	SEC
	LDA velocity
	SBC #1
	STA velocity
.skip15then
.skipL090
.L091 ;  if velocity > 240 then velocity = 0

	LDA #240
	CMP velocity
     BCS .skipL091
.condpart17
	LDA #0
  ASL
  ASL
  ASL
  ASL
	STA velocity
.skipL091
.
 ; 

.L092 ;  if !level{3} then COLUBK = 0 else COLUBK = 154

	LDA level
	AND #8
	BNE .skipL092
.condpart18
	LDA #0
	STA COLUBK
 jmp .skipelse4
.skipL092
	LDA #154
	STA COLUBK
.skipelse4
.
 ; 

.L093 ;  on carpos goto a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15

	LDX carpos
	LDA .L093jumptablehi,x
	PHA
	LDA .L093jumptablelo,x
	PHA
	RTS
.L093jumptablehi
	.byte >(.a0-1)
	.byte >(.a1-1)
	.byte >(.a2-1)
	.byte >(.a3-1)
	.byte >(.a4-1)
	.byte >(.a5-1)
	.byte >(.a6-1)
	.byte >(.a7-1)
	.byte >(.a8-1)
	.byte >(.a9-1)
	.byte >(.a10-1)
	.byte >(.a11-1)
	.byte >(.a12-1)
	.byte >(.a13-1)
	.byte >(.a14-1)
	.byte >(.a15-1)
.L093jumptablelo
	.byte <(.a0-1)
	.byte <(.a1-1)
	.byte <(.a2-1)
	.byte <(.a3-1)
	.byte <(.a4-1)
	.byte <(.a5-1)
	.byte <(.a6-1)
	.byte <(.a7-1)
	.byte <(.a8-1)
	.byte <(.a9-1)
	.byte <(.a10-1)
	.byte <(.a11-1)
	.byte <(.a12-1)
	.byte <(.a13-1)
	.byte <(.a14-1)
	.byte <(.a15-1)
.a0 ; a0 rem 0 (due north, or up)

.L094 ;  xvelocity = 0 : yvelocity = 0 - velocity

	LDA #0
  ASL
  ASL
  ASL
  ASL
	STA xvelocity
	LDA #0
	SEC
	SBC velocity
	STA yvelocity
.L095 ;  goto skipskid

 jmp .skipskid

.a1 ; a1 rem 22.5

.L096 ;  tempvel = velocity / 8

	LDA velocity
	lsr
	lsr
	lsr
	STA tempvel
.L097 ;  xvelocity = tempvel : yvelocity = tempvel - velocity

	LDA tempvel
	STA xvelocity
	LDA tempvel
	SEC
	SBC velocity
	STA yvelocity
.L098 ;  tempvel = velocity / 4

	LDA velocity
	lsr
	lsr
	STA tempvel
.L099 ;  xvelocity = xvelocity + tempvel

	LDA xvelocity
	CLC
	ADC tempvel
	STA xvelocity
.L0100 ;  goto skipskid

 jmp .skipskid

.a2 ; a2 rem 45

.L0101 ;  tempvel = velocity / 4

	LDA velocity
	lsr
	lsr
	STA tempvel
.L0102 ;  xvelocity = velocity / 2

	LDA velocity
	lsr
	STA xvelocity
.L0103 ;  if xvelocity{7} then xvelocity = xvelocity  |  %10000000

	BIT xvelocity
	BPL .skipL0103
.condpart19
	LDA xvelocity
	ORA #%10000000
	STA xvelocity
.skipL0103
.L0104 ;  xvelocity = xvelocity + tempvel : yvelocity = 0 - xvelocity

	LDA xvelocity
	CLC
	ADC tempvel
	STA xvelocity
	LDA #0
	SEC
	SBC xvelocity
	STA yvelocity
.L0105 ;  goto skipskid

 jmp .skipskid

.a3 ; a3 rem 67.5

.L0106 ;  tempvel = velocity / 8

	LDA velocity
	lsr
	lsr
	lsr
	STA tempvel
.L0107 ;  xvelocity = velocity - tempvel : yvelocity = 0 - tempvel

	LDA velocity
	SEC
	SBC tempvel
	STA xvelocity
	LDA #0
	SEC
	SBC tempvel
	STA yvelocity
.L0108 ;  tempvel = velocity / 4

	LDA velocity
	lsr
	lsr
	STA tempvel
.L0109 ;  yvelocity = yvelocity - tempvel

	LDA yvelocity
	SEC
	SBC tempvel
	STA yvelocity
.L0110 ;  goto skipskid

 jmp .skipskid

.a4 ; a4 rem 90

.L0111 ;  xvelocity = velocity : yvelocity = 0

	LDA velocity
	STA xvelocity
	LDA #0
  ASL
  ASL
  ASL
  ASL
	STA yvelocity
.L0112 ;  goto skipskid

 jmp .skipskid

.a5 ; a5 rem 112.5

.L0113 ;  tempvel = velocity / 8

	LDA velocity
	lsr
	lsr
	lsr
	STA tempvel
.L0114 ;  xvelocity = velocity - tempvel : yvelocity = tempvel

	LDA velocity
	SEC
	SBC tempvel
	STA xvelocity
	LDA tempvel
	STA yvelocity
.L0115 ;  tempvel = velocity / 4

	LDA velocity
	lsr
	lsr
	STA tempvel
.L0116 ;  yvelocity = yvelocity + tempvel

	LDA yvelocity
	CLC
	ADC tempvel
	STA yvelocity
.L0117 ;  goto skipskid

 jmp .skipskid

.a6 ; a6 rem 135

.L0118 ;  tempvel = velocity / 4

	LDA velocity
	lsr
	lsr
	STA tempvel
.L0119 ;  xvelocity = velocity / 2

	LDA velocity
	lsr
	STA xvelocity
.L0120 ;  if xvelocity{7} then xvelocity = xvelocity  |  %10000000

	BIT xvelocity
	BPL .skipL0120
.condpart20
	LDA xvelocity
	ORA #%10000000
	STA xvelocity
.skipL0120
.L0121 ;  xvelocity = xvelocity + tempvel : yvelocity = xvelocity

	LDA xvelocity
	CLC
	ADC tempvel
	STA xvelocity
	LDA xvelocity
	STA yvelocity
.L0122 ;  goto skipskid

 jmp .skipskid

.a7 ; a7 rem 157.5

.L0123 ;  tempvel = velocity / 8

	LDA velocity
	lsr
	lsr
	lsr
	STA tempvel
.L0124 ;  xvelocity = tempvel : yvelocity = velocity - tempvel

	LDA tempvel
	STA xvelocity
	LDA velocity
	SEC
	SBC tempvel
	STA yvelocity
.L0125 ;  tempvel = velocity / 4

	LDA velocity
	lsr
	lsr
	STA tempvel
.L0126 ;  xvelocity = xvelocity + tempvel

	LDA xvelocity
	CLC
	ADC tempvel
	STA xvelocity
.L0127 ;  goto skipskid

 jmp .skipskid

.a8 ; a8 rem 180

.L0128 ;  xvelocity = 0 : yvelocity = velocity

	LDA #0
  ASL
  ASL
  ASL
  ASL
	STA xvelocity
	LDA velocity
	STA yvelocity
.L0129 ;  goto skipskid

 jmp .skipskid

.a9 ; a9 rem 202.5

.L0130 ;  tempvel = velocity / 8

	LDA velocity
	lsr
	lsr
	lsr
	STA tempvel
.L0131 ;  xvelocity = 0 - tempvel : yvelocity = velocity - tempvel

	LDA #0
	SEC
	SBC tempvel
	STA xvelocity
	LDA velocity
	SEC
	SBC tempvel
	STA yvelocity
.L0132 ;  tempvel = velocity / 4

	LDA velocity
	lsr
	lsr
	STA tempvel
.L0133 ;  xvelocity = xvelocity - tempvel

	LDA xvelocity
	SEC
	SBC tempvel
	STA xvelocity
.L0134 ;  goto skipskid

 jmp .skipskid

.a10 ; a10 rem 225

.L0135 ;  tempvel = velocity / 4

	LDA velocity
	lsr
	lsr
	STA tempvel
.L0136 ;  xvelocity = velocity / 2

	LDA velocity
	lsr
	STA xvelocity
.L0137 ;  yvelocity = tempvel + xvelocity : xvelocity = 0 - xvelocity

	LDA tempvel
	CLC
	ADC xvelocity
	STA yvelocity
	LDA #0
	SEC
	SBC xvelocity
	STA xvelocity
.L0138 ;  goto skipskid

 jmp .skipskid

.a11 ; a11 rem 247.5

.L0139 ;  tempvel = velocity / 8

	LDA velocity
	lsr
	lsr
	lsr
	STA tempvel
.L0140 ;  xvelocity = tempvel - velocity : yvelocity = tempvel

	LDA tempvel
	SEC
	SBC velocity
	STA xvelocity
	LDA tempvel
	STA yvelocity
.L0141 ;  tempvel = velocity / 4

	LDA velocity
	lsr
	lsr
	STA tempvel
.L0142 ;  yvelocity = yvelocity + tempvel

	LDA yvelocity
	CLC
	ADC tempvel
	STA yvelocity
.L0143 ;  goto skipskid

 jmp .skipskid

.a12 ; a12 rem 270

.L0144 ;  xvelocity = 0 - velocity : yvelocity = 0

	LDA #0
	SEC
	SBC velocity
	STA xvelocity
	LDA #0
  ASL
  ASL
  ASL
  ASL
	STA yvelocity
.L0145 ;  goto skipskid

 jmp .skipskid

.a13 ; a13 rem 292.5

.L0146 ;  tempvel = velocity / 8

	LDA velocity
	lsr
	lsr
	lsr
	STA tempvel
.L0147 ;  xvelocity = tempvel - velocity : yvelocity = 0 - tempvel

	LDA tempvel
	SEC
	SBC velocity
	STA xvelocity
	LDA #0
	SEC
	SBC tempvel
	STA yvelocity
.L0148 ;  tempvel = velocity / 4

	LDA velocity
	lsr
	lsr
	STA tempvel
.L0149 ;  yvelocity = yvelocity - tempvel

	LDA yvelocity
	SEC
	SBC tempvel
	STA yvelocity
.L0150 ;  goto skipskid

 jmp .skipskid

.a14 ; a14 rem 315

.L0151 ;  tempvel = velocity / 4

	LDA velocity
	lsr
	lsr
	STA tempvel
.L0152 ;  xvelocity = velocity / 2

	LDA velocity
	lsr
	STA xvelocity
.L0153 ;  xvelocity = tempvel + xvelocity : xvelocity = 0 - xvelocity : yvelocity = xvelocity

	LDA tempvel
	CLC
	ADC xvelocity
	STA xvelocity
	LDA #0
	SEC
	SBC xvelocity
	STA xvelocity
	LDA xvelocity
	STA yvelocity
.L0154 ;  goto skipskid

 jmp .skipskid

.a15 ; a15 rem 337.5 

.L0155 ;  tempvel = velocity / 8

	LDA velocity
	lsr
	lsr
	lsr
	STA tempvel
.L0156 ;  xvelocity = 0 - tempvel : yvelocity = tempvel - velocity

	LDA #0
	SEC
	SBC tempvel
	STA xvelocity
	LDA tempvel
	SEC
	SBC velocity
	STA yvelocity
.L0157 ;  tempvel = velocity / 4

	LDA velocity
	lsr
	lsr
	STA tempvel
.L0158 ;  xvelocity = xvelocity - tempvel

	LDA xvelocity
	SEC
	SBC tempvel
	STA xvelocity
.
 ; 

.
 ; 

.skipskid
 ; skipskid

.L0159 ;  if velocity{7} then reboot

	BIT velocity
	BPL .skipL0159
.condpart21
	JMP ($FFFC)
.skipL0159
.L0160 ;  if !gamebits{0} then finalxvelocity = xvelocity : finalyvelocity = yvelocity : AUDV1 = 0 : goto noskid else skid

	LDA gamebits
	LSR
	BCS .skipL0160
.condpart22
	LDA xvelocity
	STA finalxvelocity
	LDA yvelocity
	STA finalyvelocity
	LDA #0
	STA AUDV1
 jmp .noskid
.skipL0160
 jmp .skid

.skipelse5
.L0161 ;  if velocity < 32 then finalxvelocity = xvelocity : finalyvelocity = yvelocity : AUDV1 = 0 : goto noskid

	LDA velocity
	CMP #32
     BCS .skipL0161
.condpart23
	LDA xvelocity
	STA finalxvelocity
	LDA yvelocity
	STA finalyvelocity
	LDA #0
	STA AUDV1
 jmp .noskid

.skipL0161
.L0162 ;  if finalxvelocity  =  xvelocity  &&  finalyvelocity  =  yvelocity then AUDV1 = 0 : goto noskid

	LDA finalxvelocity
	CMP xvelocity
     BNE .skipL0162
.condpart24
	LDA finalyvelocity
	CMP yvelocity
     BNE .skip24then
.condpart25
	LDA #0
	STA AUDV1
 jmp .noskid

.skip24then
.skipL0162
.
 ; 

.skid
 ; skid

.L0163 ;  rem lost traction...skid

.L0164 ;  gamebits{5} = 0

	LDA gamebits
	AND #223
	STA gamebits
.L0165 ;  gamebits{6} = 0

	LDA gamebits
	AND #191
	STA gamebits
.
 ; 

.L0166 ;  if xvelocity > 127  &&  finalxvelocity > 127 then bothxneg

	LDA #127
	CMP xvelocity
     BCS .skipL0166
.condpart26
	LDA #127
	CMP finalxvelocity
 if ((* - .bothxneg) < 127) && ((* - .bothxneg) > -128)
	bcc .bothxneg
 else
	bcs .6skipbothxneg
	jmp .bothxneg
.6skipbothxneg
 endif
.skipL0166
.L0167 ;  if xvelocity < 128  &&  finalxvelocity < 128 then bothxpos

	LDA xvelocity
	CMP #128
     BCS .skipL0167
.condpart27
	LDA finalxvelocity
	CMP #128
 if ((* - .bothxpos) < 127) && ((* - .bothxpos) > -128)
	bcc .bothxpos
 else
	bcs .7skipbothxpos
	jmp .bothxpos
.7skipbothxpos
 endif
.skipL0167
.L0168 ;  if xvelocity > 127 then subx else addx

	LDA #127
	CMP xvelocity
 if ((* - .subx) < 127) && ((* - .subx) > -128)
	bcc .subx
 else
	bcs .8skipsubx
	jmp .subx
.8skipsubx
 endif
 jmp .addx

.skipelse6
.bothxneg
 ; bothxneg

.L0169 ;  temp1 =  ( finalxvelocity  ^  xvelocity )   &  %11111100

; complex statement detected
	LDA finalxvelocity
	EOR xvelocity
	AND #%11111100
	STA temp1
.L0170 ;  if temp1 = 0 then finalxvelocity = xvelocity : gamebits{5} = 1 : goto checky

	LDA temp1
	CMP #0
     BNE .skipL0170
.condpart28
	LDA xvelocity
	STA finalxvelocity
	LDA gamebits
	ORA #32
	STA gamebits
 jmp .checky

.skipL0170
.L0171 ;  if finalxvelocity < xvelocity then addx

	LDA finalxvelocity
	CMP xvelocity
 if ((* - .addx) < 127) && ((* - .addx) > -128)
	bcc .addx
 else
	bcs .9skipaddx
	jmp .addx
.9skipaddx
 endif
.subx
 ; subx

.L0172 ;  if level{3} then finalxvelocity = finalxvelocity - 0.0625 else finalxvelocity = finalxvelocity - 0.3

	LDA level
	AND #8
	BEQ .skipL0172
.condpart29
	SEC
	LDA finalxvelocity
	SBC #1
	STA finalxvelocity
 jmp .skipelse7
.skipL0172
	SEC
	LDA finalxvelocity
	SBC #4
	STA finalxvelocity
.skipelse7
.L0173 ;  goto checky

 jmp .checky

.
 ; 

.bothxpos
 ; bothxpos

.L0174 ;  temp1 =  ( finalxvelocity  ^  xvelocity )   &  %11111100

; complex statement detected
	LDA finalxvelocity
	EOR xvelocity
	AND #%11111100
	STA temp1
.L0175 ;  if temp1 = 0 then finalxvelocity = xvelocity : gamebits{5} = 1 : goto checky

	LDA temp1
	CMP #0
     BNE .skipL0175
.condpart30
	LDA xvelocity
	STA finalxvelocity
	LDA gamebits
	ORA #32
	STA gamebits
 jmp .checky

.skipL0175
.L0176 ;  if finalxvelocity > xvelocity then subx

	LDA xvelocity
	CMP finalxvelocity
 if ((* - .subx) < 127) && ((* - .subx) > -128)
	bcc .subx
 else
	bcs .10skipsubx
	jmp .subx
.10skipsubx
 endif
.addx
 ; addx

.L0177 ;  if level{3} then finalxvelocity = finalxvelocity + 0.0625 else finalxvelocity = finalxvelocity + 0.3

	LDA level
	AND #8
	BEQ .skipL0177
.condpart31
	CLC
	LDA finalxvelocity
	ADC #1
	STA finalxvelocity
 jmp .skipelse8
.skipL0177
	CLC
	LDA finalxvelocity
	ADC #4
	STA finalxvelocity
.skipelse8
.
 ; 

.
 ; 

.checky
 ; checky

.L0178 ;  if yvelocity > 127  &&  finalyvelocity > 127 then bothyneg

	LDA #127
	CMP yvelocity
     BCS .skipL0178
.condpart32
	LDA #127
	CMP finalyvelocity
 if ((* - .bothyneg) < 127) && ((* - .bothyneg) > -128)
	bcc .bothyneg
 else
	bcs .11skipbothyneg
	jmp .bothyneg
.11skipbothyneg
 endif
.skipL0178
.L0179 ;  if yvelocity < 128  &&  finalyvelocity < 128 then bothypos

	LDA yvelocity
	CMP #128
     BCS .skipL0179
.condpart33
	LDA finalyvelocity
	CMP #128
 if ((* - .bothypos) < 127) && ((* - .bothypos) > -128)
	bcc .bothypos
 else
	bcs .12skipbothypos
	jmp .bothypos
.12skipbothypos
 endif
.skipL0179
.L0180 ;  if yvelocity > 127 then suby else addy

	LDA #127
	CMP yvelocity
 if ((* - .suby) < 127) && ((* - .suby) > -128)
	bcc .suby
 else
	bcs .13skipsuby
	jmp .suby
.13skipsuby
 endif
 jmp .addy

.skipelse9
.bothyneg
 ; bothyneg

.L0181 ;  temp1 =  ( finalyvelocity  ^  yvelocity )   &  %11111100

; complex statement detected
	LDA finalyvelocity
	EOR yvelocity
	AND #%11111100
	STA temp1
.L0182 ;  if temp1 = 0 then finalyvelocity = yvelocity : gamebits{6} = 1 : goto doneskid

	LDA temp1
	CMP #0
     BNE .skipL0182
.condpart34
	LDA yvelocity
	STA finalyvelocity
	LDA gamebits
	ORA #64
	STA gamebits
 jmp .doneskid

.skipL0182
.L0183 ;  if finalyvelocity < yvelocity then addy

	LDA finalyvelocity
	CMP yvelocity
 if ((* - .addy) < 127) && ((* - .addy) > -128)
	bcc .addy
 else
	bcs .14skipaddy
	jmp .addy
.14skipaddy
 endif
.suby
 ; suby

.L0184 ;  if level{3} then finalyvelocity = finalyvelocity - 0.0625 else finalyvelocity = finalyvelocity - 0.3

	LDA level
	AND #8
	BEQ .skipL0184
.condpart35
	SEC
	LDA finalyvelocity
	SBC #1
	STA finalyvelocity
 jmp .skipelse10
.skipL0184
	SEC
	LDA finalyvelocity
	SBC #4
	STA finalyvelocity
.skipelse10
.L0185 ;  goto doneskid

 jmp .doneskid

.
 ; 

.bothypos
 ; bothypos

.L0186 ;  temp1 =  ( finalyvelocity  ^  yvelocity )   &  %11111100

; complex statement detected
	LDA finalyvelocity
	EOR yvelocity
	AND #%11111100
	STA temp1
.L0187 ;  if temp1 = 0 then finalyvelocity = yvelocity : gamebits{6} = 1 : goto doneskid

	LDA temp1
	CMP #0
     BNE .skipL0187
.condpart36
	LDA yvelocity
	STA finalyvelocity
	LDA gamebits
	ORA #64
	STA gamebits
 jmp .doneskid

.skipL0187
.L0188 ;  if finalyvelocity > yvelocity then suby

	LDA yvelocity
	CMP finalyvelocity
 if ((* - .suby) < 127) && ((* - .suby) > -128)
	bcc .suby
 else
	bcs .15skipsuby
	jmp .suby
.15skipsuby
 endif
.addy
 ; addy

.L0189 ;  if level{3} then finalyvelocity = finalyvelocity + 0.0625 else finalyvelocity = finalyvelocity + 0.3

	LDA level
	AND #8
	BEQ .skipL0189
.condpart37
	CLC
	LDA finalyvelocity
	ADC #1
	STA finalyvelocity
 jmp .skipelse11
.skipL0189
	CLC
	LDA finalyvelocity
	ADC #4
	STA finalyvelocity
.skipelse11
.
 ; 

.doneskid
 ; doneskid

.L0190 ;  if gamebits{5}  &&  gamebits{6} then gamebits{0} = 0 : AUDV1 = 0 : goto noskid

	LDA gamebits
	AND #32
	BEQ .skipL0190
.condpart38
	BIT gamebits
	BVC .skip38then
.condpart39
	LDA gamebits
	AND #254
	STA gamebits
	LDA #0
	STA AUDV1
 jmp .noskid

.skip38then
.skipL0190
.
 ; 

.L0191 ;  rem skid sound

.L0192 ;  temp6 = rand

 jsr randomize
	STA temp6
.L0193 ;  if temp1{6} then AUDV1 = 9

	BIT temp1
	BVC .skipL0193
.condpart40
	LDA #9
	STA AUDV1
.skipL0193
.
 ; 

.L0194 ;  rem if temp6{0} then AUDC1=3:AUDF1=0 else AUDC1=3:AUDF1=1

.L0195 ;  rem AUDV1=temp6&3

.L0196 ;  if level{3} then temp1 = 8 else temp1 = 20

	LDA level
	AND #8
	BEQ .skipL0196
.condpart41
	LDA #8
	STA temp1
 jmp .skipelse12
.skipL0196
	LDA #20
	STA temp1
.skipelse12
.L0197 ;  AUDC1 = temp1

	LDA temp1
	STA AUDC1
.L0198 ;  if temp6{0} then AUDF1 = temp1 + 4 else AUDF1 = temp1 + 5

	LDA temp6
	LSR
	BCC .skipL0198
.condpart42
	LDA temp1
	CLC
	ADC #4
	STA AUDF1
 jmp .skipelse13
.skipL0198
	LDA temp1
	CLC
	ADC #5
	STA AUDF1
.skipelse13
.
 ; 

.
 ; 

.noskid
 ; noskid

.L0199 ;  rem gamebits=gamebits^%00000010

.L0200 ;  rem if gamebits{1} then donotadd

.
 ; 

.L0201 ;  tempvel8 = finalxvelocity

	LDA finalxvelocity
  JSR Assign44to88
  STX temp2
	STA tempvel8
.L0202 ;  asm

 lda temp1

 asl

 ror temp1

 ror temp2

.L0203 ;  p0x = p0x + tempvel8

	LDA j
	CLC 
	ADC temp2
	STA j
	LDA p0x
	ADC tempvel8
	STA p0x
.
 ; 

.L0204 ;  tempvel8 = finalyvelocity

	LDA finalyvelocity
  JSR Assign44to88
  STX temp2
	STA tempvel8
.L0205 ;  asm

 lda temp1

 asl

 ror temp1

 ror temp2

.
 ; 

.L0206 ;  p0y = p0y + tempvel8

	LDA k
	CLC 
	ADC temp2
	STA k
	LDA p0y
	ADC tempvel8
	STA p0y
.donotadd
 ; donotadd

.
 ; 

.L0207 ;  if player0x > 200 then player0x = 159 : goto wrap

	LDA #200
	CMP player0x
     BCS .skipL0207
.condpart43
	LDA #159
	STA player0x
 jmp .wrap

.skipL0207
.L0208 ;  if player0x > 159 then player0x = 0

	LDA #159
	CMP player0x
     BCS .skipL0208
.condpart44
	LDA #0
	STA player0x
.skipL0208
.wrap
 ; wrap

.L0209 ;  if player0y > 200 then player0y = 96

	LDA #200
	CMP player0y
     BCS .skipL0209
.condpart45
	LDA #96
	STA player0y
.skipL0209
.L0210 ;  if player0y > 96 then player0y = 0

	LDA #96
	CMP player0y
     BCS .skipL0210
.condpart46
	LDA #0
	STA player0y
.skipL0210
.
 ; 

.L0211 ;  if switchbw then driving

 lda #8
 bit SWCHB
 if ((* - .driving) < 127) && ((* - .driving) > -128)
	BEQ .driving
 else
	bne .16skipdriving
	jmp .driving
.16skipdriving
 endif
.L0212 ;  turndelay  =  turndelay  +  1

	INC turndelay
.L0213 ;  turndelay  =  turndelay  &  %11111011

	LDA turndelay
	AND #%11111011
	STA turndelay
.L0214 ;  temp1 = turndelay & 3

	LDA turndelay
	AND #3
	STA temp1
.L0215 ;  if temp1  <>  0 goto SameFrame

	LDA temp1
	CMP #0
 if ((* - .SameFrame) < 127) && ((* - .SameFrame) > -128)
	BNE .SameFrame
 else
	beq .17skipSameFrame
	jmp .SameFrame
.17skipSameFrame
 endif
.
 ; 

.
 ; 

.L0216 ;  if joy0left then carpos = carpos - 1 : gamebits{0} = 1

 lda #$40
 bit SWCHA
	BNE .skipL0216
.condpart47
	DEC carpos
	LDA gamebits
	ORA #1
	STA gamebits
.skipL0216
.L0217 ;  if joy0right then carpos = carpos + 1 : gamebits{0} = 1

 lda #$80
 bit SWCHA
	BNE .skipL0217
.condpart48
	INC carpos
	LDA gamebits
	ORA #1
	STA gamebits
.skipL0217
.L0218 ;  goto nodriving

 jmp .nodriving

.
 ; 

.driving ; driving rem read driving controller

.L0219 ;  temp1 = SWCHA  &  %00110000

	LDA SWCHA
	AND #%00110000
	STA temp1
.L0220 ;  temp1 = temp1 / 16

	LDA temp1
	lsr
	lsr
	lsr
	lsr
	STA temp1
.L0221 ;  on last goto d00 d01 d10 d11

	LDX last
	LDA .L0221jumptablehi,x
	PHA
	LDA .L0221jumptablelo,x
	PHA
	RTS
.L0221jumptablehi
	.byte >(.d00-1)
	.byte >(.d01-1)
	.byte >(.d10-1)
	.byte >(.d11-1)
.L0221jumptablelo
	.byte <(.d00-1)
	.byte <(.d01-1)
	.byte <(.d10-1)
	.byte <(.d11-1)
.d00 ; d00 on temp1 goto nomove left right nomove

	LDX temp1
	LDA .d00jumptablehi,x
	PHA
	LDA .d00jumptablelo,x
	PHA
	RTS
.d00jumptablehi
	.byte >(.nomove-1)
	.byte >(.left-1)
	.byte >(.right-1)
	.byte >(.nomove-1)
.d00jumptablelo
	.byte <(.nomove-1)
	.byte <(.left-1)
	.byte <(.right-1)
	.byte <(.nomove-1)
.d01 ; d01 on temp1 goto right nomove nomove left

	LDX temp1
	LDA .d01jumptablehi,x
	PHA
	LDA .d01jumptablelo,x
	PHA
	RTS
.d01jumptablehi
	.byte >(.right-1)
	.byte >(.nomove-1)
	.byte >(.nomove-1)
	.byte >(.left-1)
.d01jumptablelo
	.byte <(.right-1)
	.byte <(.nomove-1)
	.byte <(.nomove-1)
	.byte <(.left-1)
.d11 ; d11 on temp1 goto nomove right left nomove

	LDX temp1
	LDA .d11jumptablehi,x
	PHA
	LDA .d11jumptablelo,x
	PHA
	RTS
.d11jumptablehi
	.byte >(.nomove-1)
	.byte >(.right-1)
	.byte >(.left-1)
	.byte >(.nomove-1)
.d11jumptablelo
	.byte <(.nomove-1)
	.byte <(.right-1)
	.byte <(.left-1)
	.byte <(.nomove-1)
.d10 ; d10 on temp1 goto left nomove nomove right

	LDX temp1
	LDA .d10jumptablehi,x
	PHA
	LDA .d10jumptablelo,x
	PHA
	RTS
.d10jumptablehi
	.byte >(.left-1)
	.byte >(.nomove-1)
	.byte >(.nomove-1)
	.byte >(.right-1)
.d10jumptablelo
	.byte <(.left-1)
	.byte <(.nomove-1)
	.byte <(.nomove-1)
	.byte <(.right-1)
.L0222 ;  rem done with reading code

.left ; left carpos = carpos - 1 : gamebits{0} = 1

	DEC carpos
	LDA gamebits
	ORA #1
	STA gamebits
.L0223 ;  goto nomove

 jmp .nomove

.right ; right carpos = carpos + 1 : gamebits{0} = 1

	INC carpos
	LDA gamebits
	ORA #1
	STA gamebits
.nomove
 ; nomove

.L0224 ;  last = temp1

	LDA temp1
	STA last
.nodriving
 ; nodriving

.L0225 ;  carpos = carpos  &  15

	LDA carpos
	AND #15
	STA carpos
.
 ; 

.L0226 ;  gosub carFrame

 jsr .carFrame

.
 ; 

.SameFrame
 ; SameFrame

.L0227 ;  COLUP0  =  14

	LDA #14
	STA COLUP0
.L0228 ;  REFP0 = gamebits

	LDA gamebits
	STA REFP0
.L0229 ;  if scadd > 0 then scadd = scadd - 1 : score = score + 1

	LDA #0
	CMP scadd
     BCS .skipL0229
.condpart49
	DEC scadd
	SED
	CLC
	LDA score+2
	ADC #$01
	STA score+2
	LDA score+1
	ADC #$00
	STA score+1
	LDA score
	ADC #$00
	STA score
	CLD
.skipL0229
.
 ; 

.L0230 ;  if !collision(player0,player1) then nohitpikachu

	BIT CXPPMM
 if ((* - .nohitpikachu) < 127) && ((* - .nohitpikachu) > -128)
	bpl .nohitpikachu
 else
	bmi .18skipnohitpikachu
	jmp .nohitpikachu
.18skipnohitpikachu
 endif
.L0231 ;  scadd = scadd + f

	LDA scadd
	CLC
	ADC f
	STA scadd
.L0232 ;  collcount = collcount | 80

	LDA collcount
	ORA #80
	STA collcount
.L0233 ;  if player1x < 16  ||  player1x > 143 then notombstone

	LDA player1x
	CMP #16
 if ((* - .notombstone) < 127) && ((* - .notombstone) > -128)
	bcc .notombstone
 else
	bcs .19skipnotombstone
	jmp .notombstone
.19skipnotombstone
 endif
	LDA #143
	CMP player1x
 if ((* - .notombstone) < 127) && ((* - .notombstone) > -128)
	bcc .notombstone
 else
	bcs .20skipnotombstone
	jmp .notombstone
.20skipnotombstone
 endif
.L0234 ;  temp1 =  ( player1x - 16 )  / 4 : temp2 =  ( player1y - 4 )  / 8

; complex statement detected
	LDA player1x
	SEC
	SBC #16
	lsr
	lsr
	STA temp1
; complex statement detected
	LDA player1y
	SEC
	SBC #4
	lsr
	lsr
	lsr
	STA temp2
.L0235 ;  pfpixel temp1 temp2 on

	LDA temp1
	LDY temp2
	LDX #0
 jsr pfpixel
.notombstone
 ; notombstone

.L0236 ;  player1x = rand & 63 + 48 : if player1x{0} then player1y = 0 else player1y = 90

; complex statement detected
 jsr randomize
	PHA
	LDA #63
	CLC
	ADC #48
	TSX
	INX
	TXS
	AND $100,x
	STA player1x
	LDA player1x
	LSR
	BCC .skipL0236
.condpart50
	LDA #0
	STA player1y
 jmp .skipelse14
.skipL0236
	LDA #90
	STA player1y
.skipelse14
.
 ; 

.nohitpikachu
 ; nohitpikachu

.L0237 ;  if gamebits{4} then insidewall

	LDA gamebits
	AND #16
 if ((* - .insidewall) < 127) && ((* - .insidewall) > -128)
	BNE .insidewall
 else
	beq .21skipinsidewall
	jmp .insidewall
.21skipinsidewall
 endif
.L0238 ;  if collcount > 16 then hitwall

	LDA #16
	CMP collcount
 if ((* - .hitwall) < 127) && ((* - .hitwall) > -128)
	bcc .hitwall
 else
	bcs .22skiphitwall
	jmp .hitwall
.22skiphitwall
 endif
.L0239 ;  if !collision(player0,playfield) then insidewall

	BIT CXP0FB
 if ((* - .insidewall) < 127) && ((* - .insidewall) > -128)
	bpl .insidewall
 else
	bmi .23skipinsidewall
	jmp .insidewall
.23skipinsidewall
 endif
.L0240 ;  if !switchleftb then velocity = 0

 lda #$40
 bit SWCHB
	BEQ .skipL0240
.condpart51
	LDA #0
  ASL
  ASL
  ASL
  ASL
	STA velocity
.skipL0240
.L0241 ;  gamebits{4} = 1 : collcount = collcount  |  96 : collcount{3} = 0 : goto hitwall

	LDA gamebits
	ORA #16
	STA gamebits
	LDA collcount
	ORA #96
	STA collcount
	LDA collcount
	AND #247
	STA collcount
 jmp .hitwall

.insidewall
 ; insidewall

.L0242 ;  if !collision(player0,playfield) then gamebits{4} = 0 : goto hitwall

	BIT CXP0FB
	BMI .skipL0242
.condpart52
	LDA gamebits
	AND #239
	STA gamebits
 jmp .hitwall

.skipL0242
.L0243 ;  if velocity > 16 then velocity = velocity - 0.1875

	LDA #16
	CMP velocity
     BCS .skipL0243
.condpart53
	SEC
	LDA velocity
	SBC #3
	STA velocity
.skipL0243
.
 ; 

.hitwall
 ; hitwall

.L0244 ;  drawscreen

 jsr drawscreen
.
 ; 

.L0245 ;  goto startLoop

 jmp .startLoop

.
 ; 

.
 ; 

.carFrame
 ; carFrame

.
 ; 

.
 ; 

.L0246 ;  carframe  =  0

	LDA #0
	STA carframe
.L0247 ;  if carpos  <  9 then carframe  =  carpos  :  gamebits{3} =  0

	LDA carpos
	CMP #9
     BCS .skipL0247
.condpart54
	LDA carpos
	STA carframe
	LDA gamebits
	AND #247
	STA gamebits
.skipL0247
.L0248 ;  if carpos  >=  9 then carframe  =  16  -  carpos  :  gamebits{3}  =  1

	LDA carpos
	CMP #9
     BCC .skipL0248
.condpart55
	LDA #16
	SEC
	SBC carpos
	STA carframe
	LDA gamebits
	ORA #8
	STA gamebits
.skipL0248
.
 ; 

.
 ; 

.
 ; 

.
 ; 

.L0249 ;  on carframe goto 5 10 20 30 40 50 60 70 80

	LDX carframe
	LDA .L0249jumptablehi,x
	PHA
	LDA .L0249jumptablelo,x
	PHA
	RTS
.L0249jumptablehi
	.byte >(.5-1)
	.byte >(.10-1)
	.byte >(.20-1)
	.byte >(.30-1)
	.byte >(.40-1)
	.byte >(.50-1)
	.byte >(.60-1)
	.byte >(.70-1)
	.byte >(.80-1)
.L0249jumptablelo
	.byte <(.5-1)
	.byte <(.10-1)
	.byte <(.20-1)
	.byte <(.30-1)
	.byte <(.40-1)
	.byte <(.50-1)
	.byte <(.60-1)
	.byte <(.70-1)
	.byte <(.80-1)
.
 ; 

.
 ; 

.
 ; 

.
 ; 

.
 ; 

.5 ; 5 player0:

	LDA #<player5_0

	STA player0pointerlo
	LDA #>player5_0

	STA player0pointerhi
	LDA #8
	STA player0height
.L0250 ;  goto doneSetFrame

 jmp .doneSetFrame

.
 ; 

.10 ; 10 player0:

	LDA #<player10_0

	STA player0pointerlo
	LDA #>player10_0

	STA player0pointerhi
	LDA #9
	STA player0height
.L0251 ;  goto doneSetFrame

 jmp .doneSetFrame

.
 ; 

.20 ; 20 player0:

	LDA #<player20_0

	STA player0pointerlo
	LDA #>player20_0

	STA player0pointerhi
	LDA #8
	STA player0height
.L0252 ;  goto doneSetFrame

 jmp .doneSetFrame

.
 ; 

.30 ; 30 player0:

	LDA #<player30_0

	STA player0pointerlo
	LDA #>player30_0

	STA player0pointerhi
	LDA #8
	STA player0height
.L0253 ;  goto doneSetFrame

 jmp .doneSetFrame

.
 ; 

.40 ; 40 player0:

	LDA #<player40_0

	STA player0pointerlo
	LDA #>player40_0

	STA player0pointerhi
	LDA #8
	STA player0height
.L0254 ;  goto doneSetFrame

 jmp .doneSetFrame

.
 ; 

.50 ; 50 player0:

	LDA #<player50_0

	STA player0pointerlo
	LDA #>player50_0

	STA player0pointerhi
	LDA #8
	STA player0height
.L0255 ;  goto doneSetFrame

 jmp .doneSetFrame

.
 ; 

.60 ; 60 player0:

	LDA #<player60_0

	STA player0pointerlo
	LDA #>player60_0

	STA player0pointerhi
	LDA #8
	STA player0height
.L0256 ;  goto doneSetFrame

 jmp .doneSetFrame

.
 ; 

.70 ; 70 player0:

	LDA #<player70_0

	STA player0pointerlo
	LDA #>player70_0

	STA player0pointerhi
	LDA #8
	STA player0height
.L0257 ;  goto doneSetFrame

 jmp .doneSetFrame

.
 ; 

.80 ; 80 player0:

	LDA #<player80_0

	STA player0pointerlo
	LDA #>player80_0

	STA player0pointerhi
	LDA #8
	STA player0height
.doneSetFrame
 ; doneSetFrame

.
 ; 

.L0258 ;  return

	RTS
.
 ; 

.movepikachu
 ; movepikachu

.L0259 ;  temp1 = pikachuxvel & 252 : temp2 = pikachuyvel & 252 : temp3 = pikachufinalxvel & 252 : temp4 = pikachufinalyvel & 252

	LDA pikachuxvel
	AND #252
	STA temp1
	LDA pikachuyvel
	AND #252
	STA temp2
	LDA pikachufinalxvel
	AND #252
	STA temp3
	LDA pikachufinalyvel
	AND #252
	STA temp4
.L0260 ;  if temp1 <> temp3 then check80

	LDA temp1
	CMP temp3
 if ((* - .check80) < 127) && ((* - .check80) > -128)
	BNE .check80
 else
	beq .24skipcheck80
	jmp .check80
.24skipcheck80
 endif
.L0261 ;  pikachufinalxvel = rand

 jsr randomize
	STA pikachufinalxvel
.L0262 ;  if level{1} then temp1 = player1x - player0x : pikachufinalxvel{7} = temp1{7}

	LDA level
	AND #2
	BEQ .skipL0262
.condpart56
	LDA player1x
	SEC
	SBC player0x
	STA temp1
	LDA temp1
	AND #128
  PHP
	LDA pikachufinalxvel
  PLP
	.byte $D0, $03
	AND #127
	.byte $0C
	ORA #128
	STA pikachufinalxvel
.skipL0262
.check80
 ; check80

.L0263 ;  if temp2 <> temp4 then donecheck24

	LDA temp2
	CMP temp4
 if ((* - .donecheck24) < 127) && ((* - .donecheck24) > -128)
	BNE .donecheck24
 else
	beq .25skipdonecheck24
	jmp .donecheck24
.25skipdonecheck24
 endif
.L0264 ;  pikachufinalyvel = rand

 jsr randomize
	STA pikachufinalyvel
.L0265 ;  if level{1} then temp1 = player1y - player0y : pikachufinalyvel{7} = temp1{7}

	LDA level
	AND #2
	BEQ .skipL0265
.condpart57
	LDA player1y
	SEC
	SBC player0y
	STA temp1
	LDA temp1
	AND #128
  PHP
	LDA pikachufinalyvel
  PLP
	.byte $D0, $03
	AND #127
	.byte $0C
	ORA #128
	STA pikachufinalyvel
.skipL0265
.donecheck24
 ; donecheck24

.L0266 ;  if pikachuxvel{7}  &&  !pikachufinalxvel{7} then pikachuxvel = pikachuxvel + 1 : goto donex

	BIT pikachuxvel
	BPL .skipL0266
.condpart58
	BIT pikachufinalxvel
	BMI .skip58then
.condpart59
	INC pikachuxvel
 jmp .donex

.skip58then
.skipL0266
.L0267 ;  if !pikachuxvel{7}  &&  pikachufinalxvel{7} then pikachuxvel = pikachuxvel - 1 : goto donex

	BIT pikachuxvel
	BMI .skipL0267
.condpart60
	BIT pikachufinalxvel
	BPL .skip60then
.condpart61
	DEC pikachuxvel
 jmp .donex

.skip60then
.skipL0267
.L0268 ;  if pikachuxvel > pikachufinalxvel then pikachuxvel = pikachuxvel - 1 else pikachuxvel = pikachuxvel + 1

	LDA pikachufinalxvel
	CMP pikachuxvel
     BCS .skipL0268
.condpart62
	DEC pikachuxvel
 jmp .skipelse15
.skipL0268
	INC pikachuxvel
.skipelse15
.donex
 ; donex

.L0269 ;  if pikachuyvel{7}  &&  !pikachufinalyvel{7} then pikachuyvel = pikachuyvel + 1 : goto doney

	BIT pikachuyvel
	BPL .skipL0269
.condpart63
	BIT pikachufinalyvel
	BMI .skip63then
.condpart64
	INC pikachuyvel
 jmp .doney

.skip63then
.skipL0269
.L0270 ;  if !pikachuyvel{7}  &&  pikachufinalyvel{7} then pikachuyvel = pikachuyvel - 1 : goto doney

	BIT pikachuyvel
	BMI .skipL0270
.condpart65
	BIT pikachufinalyvel
	BPL .skip65then
.condpart66
	DEC pikachuyvel
 jmp .doney

.skip65then
.skipL0270
.L0271 ;  if pikachuyvel > pikachufinalyvel then pikachuyvel = pikachuyvel - 1 else pikachuyvel = pikachuyvel + 1

	LDA pikachufinalyvel
	CMP pikachuyvel
     BCS .skipL0271
.condpart67
	DEC pikachuyvel
 jmp .skipelse16
.skipL0271
	INC pikachuyvel
.skipelse16
.doney
 ; doney

.L0272 ;  temp5 = 0

	LDA #0
	STA temp5
.L0273 ;  if pikachuxvel{7} then temp5 = 255

	BIT pikachuxvel
	BPL .skipL0273
.condpart68
	LDA #255
	STA temp5
.skipL0273
.L0274 ;  temp3 = 0

	LDA #0
	STA temp3
.L0275 ;  if pikachuyvel{7} then temp3 = 255

	BIT pikachuyvel
	BPL .skipL0275
.condpart69
	LDA #255
	STA temp3
.skipL0275
.L0276 ;  temp6 = pikachuxvel : temp4 = pikachuyvel

	LDA pikachuxvel
	STA temp6
	LDA pikachuyvel
	STA temp4
.L0277 ;  pikachuxpos = pikachuxpos + pikachuvel

	LDA v
	CLC 
	ADC temp6
	STA v
	LDA pikachuxpos
	ADC pikachuvel
	STA pikachuxpos
.L0278 ;  temp6 = temp4 : temp5 = temp3

	LDA temp4
	STA temp6
	LDA temp3
	STA temp5
.L0279 ;  pikachuypos = pikachuypos + pikachuvel

	LDA w
	CLC 
	ADC temp6
	STA w
	LDA pikachuypos
	ADC pikachuvel
	STA pikachuypos
.L0280 ;  if player1y > 100 then player1y = 0

	LDA #100
	CMP player1y
     BCS .skipL0280
.condpart70
	LDA #0
	STA player1y
.skipL0280
.L0281 ;  if player1y > $50 then pikachufinalyvel =  ( pikachufinalyvel ^ 127 )  | 128 : pikachuyvel =  ( pikachuyvel ^ 127 )  | 128

	LDA #$50
	CMP player1y
     BCS .skipL0281
.condpart71
; complex statement detected
	LDA pikachufinalyvel
	EOR #127
	ORA #128
	STA pikachufinalyvel
; complex statement detected
	LDA pikachuyvel
	EOR #127
	ORA #128
	STA pikachuyvel
.skipL0281
.L0282 ;  if player1y < 10 then pikachufinalyvel =  ( pikachufinalyvel ^ 127 )  & 127 : pikachuyvel =  ( pikachuyvel ^ 127 )  & 127

	LDA player1y
	CMP #10
     BCS .skipL0282
.condpart72
; complex statement detected
	LDA pikachufinalyvel
	EOR #127
	AND #127
	STA pikachufinalyvel
; complex statement detected
	LDA pikachuyvel
	EOR #127
	AND #127
	STA pikachuyvel
.skipL0282
.L0283 ;  if player1x > 200 then player1x = player1x + 160

	LDA #200
	CMP player1x
     BCS .skipL0283
.condpart73
	LDA player1x
	CLC
	ADC #160
	STA player1x
.skipL0283
.L0284 ;  if player1x > 160 then player1x = player1x - 160

	LDA #160
	CMP player1x
     BCS .skipL0284
.condpart74
	LDA player1x
	SEC
	SBC #160
	STA player1x
.skipL0284
.L0285 ;  REFP1 = pikachuxvel / 16

	LDA pikachuxvel
	lsr
	lsr
	lsr
	lsr
	STA REFP1
.L0286 ;  return

	RTS
.
 ; 

.L0287 ;  data gonextlevel

	JMP .skipL0287
gonextlevel
	.byte  1,2,3,4,5,6,7,8,9,$10,$11,$12,$13,$14,$15,$99

.skipL0287
.
 ; 

.L0288 ;  vblank

vblank_bB_code
.L0289 ;  if gamebits{7}  &&  level{0} then gosub movepikachu

	BIT gamebits
	BPL .skipL0289
.condpart75
	LDA level
	LSR
	BCC .skip75then
.condpart76
 jsr .movepikachu

.skip75then
.skipL0289
.L0290 ;  return

	RTS
 if (<*) > (<(*+9))
	repeat ($100-<*)
	.byte 0
	repend
	endif
player0then_1

	.byte 0
	.byte         %00100100
	.byte         %00011000
	.byte         %00011000
	.byte         %00111100
	.byte         %00100100
	.byte         %00011000
	.byte         %00100100
	.byte         %01000010
 if (<*) > (<(*+9))
	repeat ($100-<*)
	.byte 0
	repend
	endif
player1then_1

	.byte 0
	.byte         %00100100
	.byte         %00011000
	.byte         %00011000
	.byte         %00111100
	.byte         %00100100
	.byte         %00011000
	.byte         %00100100
	.byte         %01000010
 if (<*) > (<(*+9))
	repeat ($100-<*)
	.byte 0
	repend
	endif
playercolorL056_1

	.byte 0
	.byte  32
	.byte  30
	.byte  30
	.byte  30
	.byte  64
	.byte  30
	.byte  30
	.byte  30
 if (<*) > (<(*+9))
	repeat ($100-<*)
	.byte 0
	repend
	endif
player5_0

	.byte 0
	.byte   %11000011
	.byte   %11111111
	.byte   %11011011
	.byte   %00011000
	.byte   %11011011
	.byte   %11111111
	.byte   %11011011
	.byte   %00011000
 if (<*) > (<(*+10))
	repeat ($100-<*)
	.byte 0
	repend
	endif
player10_0

	.byte 0
	.byte  %00000110
	.byte  %00111110
	.byte  %11110000
	.byte  %11011011
	.byte  %00011111
	.byte  %11111000
	.byte  %11001100
	.byte  %00000100
	.byte 
 if (<*) > (<(*+9))
	repeat ($100-<*)
	.byte 0
	repend
	endif
player20_0

	.byte 0
	.byte  %00001100
	.byte  %00001100
	.byte  %00110011
	.byte  %00111011
	.byte  %11011100
	.byte  %11001100
	.byte  %00110010
	.byte  %00110000
 if (<*) > (<(*+9))
	repeat ($100-<*)
	.byte 0
	repend
	endif
player30_0

	.byte 0
	.byte  %00110110
	.byte  %00110110
	.byte  %01100100
	.byte  %01111110
	.byte  %01011110
	.byte  %11001011
	.byte  %11011000
	.byte  %00011000
 if (<*) > (<(*+9))
	repeat ($100-<*)
	.byte 0
	repend
	endif
player40_0

	.byte 0
	.byte  %11101110
	.byte  %11101110
	.byte  %01000100
	.byte  %01111111
	.byte  %01111111
	.byte  %01000100
	.byte  %11101110
	.byte  %11101110
 if (<*) > (<(*+9))
	repeat ($100-<*)
	.byte 0
	repend
	endif
player50_0

	.byte 0
	.byte  %00011000
	.byte  %11011000
	.byte  %11001011
	.byte  %01011110
	.byte  %01111110
	.byte  %01100100
	.byte  %00110110
	.byte  %00110110
 if (<*) > (<(*+9))
	repeat ($100-<*)
	.byte 0
	repend
	endif
player60_0

	.byte 0
	.byte  %00110000
	.byte  %00110010
	.byte  %11001100
	.byte  %11011100
	.byte  %00111011
	.byte  %00110011
	.byte  %00001100
	.byte  %00001100
 if (<*) > (<(*+9))
	repeat ($100-<*)
	.byte 0
	repend
	endif
player70_0

	.byte 0
	.byte  %00000100
	.byte  %11001100
	.byte  %11111000
	.byte  %00011111
	.byte  %11011011
	.byte  %11110000
	.byte  %00111110
	.byte  %00000110
 if (<*) > (<(*+9))
	repeat ($100-<*)
	.byte 0
	repend
	endif
player80_0

	.byte 0
	.byte   %00011000
	.byte   %11011011
	.byte   %11111111
	.byte   %11011011
	.byte   %00011000
	.byte   %11011011
	.byte   %11111111
	.byte   %11000011
       echo "    ",[(scoretable - *)]d , "bytes of ROM space left")
 
 
 
; feel free to modify the score graphics - just keep each digit 8 high
; and keep the conditional compilation stuff intact
 ifconst ROM2k
   ORG $F7AC
 else
   ifconst bankswitch
     if bankswitch == 8
       ORG $2F94-bscode_length
       RORG $FF94-bscode_length
     endif
     if bankswitch == 16
       ORG $4F94-bscode_length
       RORG $FF94-bscode_length
     endif
     if bankswitch == 32
       ORG $8F94-bscode_length
       RORG $FF94-bscode_length
     endif
   else
     ORG $FF9C
   endif
 endif


scoretable
       .byte %00111100
       .byte %01100110
       .byte %01100110
       .byte %01100110
       .byte %01100110
       .byte %01100110
       .byte %01100110
       .byte %00111100

       .byte %01111110
       .byte %00011000
       .byte %00011000
       .byte %00011000
       .byte %00011000
       .byte %00111000
       .byte %00011000
       .byte %00001000

       .byte %01111110
       .byte %01100000
       .byte %01100000
       .byte %00111100
       .byte %00000110
       .byte %00000110
       .byte %01000110
       .byte %00111100

       .byte %00111100
       .byte %01000110
       .byte %00000110
       .byte %00000110
       .byte %00011100
       .byte %00000110
       .byte %01000110
       .byte %00111100

       .byte %00001100
       .byte %00001100
       .byte %01111110
       .byte %01001100
       .byte %01001100
       .byte %00101100
       .byte %00011100
       .byte %00001100

       .byte %00111100
       .byte %01000110
       .byte %00000110
       .byte %00000110
       .byte %00111100
       .byte %01100000
       .byte %01100000
       .byte %01111110

       .byte %00111100
       .byte %01100110
       .byte %01100110
       .byte %01100110
       .byte %01111100
       .byte %01100000
       .byte %01100010
       .byte %00111100

       .byte %00110000
       .byte %00110000
       .byte %00110000
       .byte %00011000
       .byte %00001100
       .byte %00000110
       .byte %01000010
       .byte %00111110

       .byte %00111100
       .byte %01100110
       .byte %01100110
       .byte %01100110
       .byte %00111100
       .byte %01100110
       .byte %01100110
       .byte %00111100

       .byte %00111100
       .byte %01000110
       .byte %00000110
       .byte %00111110
       .byte %01100110
       .byte %01100110
       .byte %01100110
       .byte %00111100 


 ifconst ROM2k
   ORG $F7FC
 else
   ifconst bankswitch
     if bankswitch == 8
       ORG $2FF4-bscode_length
       RORG $FFF4-bscode_length
     endif
     if bankswitch == 16
       ORG $4FF4-bscode_length
       RORG $FFF4-bscode_length
     endif
     if bankswitch == 32
       ORG $8FF4-bscode_length
       RORG $FFF4-bscode_length
     endif
   else
     ORG $FFFC
   endif
 endif
 ifconst bankswitch
   if bankswitch == 8
     ORG $2FFC
     RORG $FFFC
   endif
   if bankswitch == 16
     ORG $4FFC
     RORG $FFFC
   endif
   if bankswitch == 32
     ORG $8FFC
     RORG $FFFC
   endif
 else
   ifconst ROM2k
     ORG $F7FC
   else
     ORG $FFFC
   endif
 endif
 .word start
 .word start
